﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Devices;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using System.Collections;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.ComponentModel;
using System.Threading;
using Curling.CurlingServer;

namespace Curling
{
    public partial class MenuPage : PhoneApplicationPage
    {
        readonly IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        Accelerometer accelerometer; //cache Misrosoft.Device.Sensors
        CurlingServiceClient CurlingService; //cache System.ServiceModel

        string TargetPage;

        bool SupressHelp = false;

        public MenuPage()
        {
            InitializeComponent();

            Assembly.Load("Microsoft.Phone.Controls"); //cache Microsoft.Phone.Controls to improve performance
            
            LocationManager.FindMyCountry();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            TargetPage = "";

            PageTitle.Opacity = 0.0;
            stackPanel.Opacity = 0.0;
            LoadingGrid.Opacity = 0.0;

            SoundManager soundManager = new SoundManager(); //Load sounds

            PhoneApplicationService.Current.State.Remove("ToMenu");

            settings.Remove("gameManager");// Delete in future version
            if (GameManager.IsExists() /*|| PhoneApplicationService.Current.State.ContainsKey("GameManager")*/ || settings.Contains("GameManager"))
            //if (!GameManager.IsExists())
            //if (!settings.Contains("gameManager"))
            {
                ResumeGameButton.IsEnabled = true;
            }
            else
            {
                ResumeGameButton.IsEnabled = false;
            }

            if (settings.Contains("DontShowHelp"))
            {
                HelpPanel.Visibility = System.Windows.Visibility.Collapsed;
                DontShowAgainCheck.IsChecked = true;
            }
            else
            {
                HelpPanel.Visibility = System.Windows.Visibility.Visible;
            }

            if (SupressHelp)
            {
                HelpPanel.Visibility = System.Windows.Visibility.Collapsed;
            }

            SupressHelp = true;
        }


        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (HelpPanel.Visibility == System.Windows.Visibility.Collapsed)
            {
                FadeIn.Begin();
            }
        }

        private void AboutButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            TargetPage = "/AboutPage.xaml";

            if (TargetPage != "")
            {
                FadeOut.Completed += new EventHandler(FadeOutAnimation_Completed);
                FadeOut.Begin();
            }
        }

        void FadeOutAnimation_Completed(object sender, EventArgs e)
        {
            FadeOut.Completed -= new EventHandler(FadeOutAnimation_Completed);

            NavigationService.Navigate(new Uri(TargetPage, UriKind.Relative));
        }

        private void ScoresButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            TargetPage = "/ScoresPage.xaml";

            if (TargetPage != "")
            {
                FadeOut.Completed += new EventHandler(FadeOutAnimation_Completed);
                FadeOut.Begin();
            }
        }

        private void NewGameButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            PhoneApplicationService.Current.State["resume"] = false;

            FadeOut.Begin();

            NavigationService.Navigate(new Uri("/VSPage.xaml", UriKind.Relative));
        }

        private void ResumeGameButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            PhoneApplicationService.Current.State["resume"] = true;
            
            BeginPlay.Begin();

            LoadingAnimation.Begin();

            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }

        private void CloseButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            HelpPanel.Visibility = System.Windows.Visibility.Collapsed;

            FadeIn.Begin();
        }

        private void DontShowAgainCheck_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            settings["DontShowHelp"] = true;
        }

        private void DontShowAgainCheck_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            settings.Remove("DontShowHelp");
        }

        private void HelpButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            FadeOut.Begin();

            HelpPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (HelpPanel.Visibility == System.Windows.Visibility.Visible)
            {
                FadeIn.Begin();
                HelpPanel.Visibility = System.Windows.Visibility.Collapsed;

                e.Cancel = true;
            }
            else
            {
                Quit();
            }
        }

        private void Quit()
        { }
    }
}
